package com.itheima.utils;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取请求的uri
        String uri = request.getRequestURI();
        //如果是登录的相关资源，则直接放行
        if(uri.contains("/login.html") || uri.contains("/login") || uri.contains("/captcha/code") || uri.contains("/updatePwd.html") || uri.contains("/updatePwd")) {
            return true;
        }
        //判断用户是否登录
        if(request.getSession().getAttribute("user") != null) {
            return true;
        }
        //转发到登录页面
        request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);
        return false;
    }
}
